package org.knight.gearbox.filter;

import org.knight.gearbox.repository.UserRepository;
import org.knight.gearbox.repository.UserRoleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.GenericFilterBean;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * 此filter 只负责拦截请求,将用户信息加载到 UserContextUtil
 * 登陆处理 可以用springsecurity 或者其他的组件
 */
@Component
public class LoginFilter extends GenericFilterBean {

    //通常情况下用户登陆之后会绑定一个session, 通过session id 可以获取用户的信息

    @Autowired
    private UserRoleRepository userRoleRepo;

    private UserRepository userRepository;

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {


        HttpServletRequest httpServletRequest = (HttpServletRequest)servletRequest;

       String id = httpServletRequest.getSession().getId();

       Long userId =(Long) httpServletRequest.getSession().getAttribute("userId");

       if (userId == null) { //首次登陆,还没有用户信息



       }
       ///


    }
}
